// pages/applyCard/applyCard.js
var app = getApp()
var currentPage=1;
//获取用户信息
var getWishUser = function (that) {
  console.info("获取用户信息")
  wx.request({
    url: app.globalData.path + '/weixin/coffee/xcx/user/getUserInfo',
    data: {
      userId: that.data.userInfo.userId
    },
    method: 'GET',
    success: function (res) {
      if (res.data.isMember == true && res.data.phone == "") {
        that.setData({
          bangdingHidden: false
        })
      }
      that.setData({
        wishUser: res.data
      })
    }
  })
}

Page({

  data: {
    wishUser: {},
    errorText:"",
    errorHidden:true,
    jihuoHidden:true,
    codeType:"",
    code:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this

    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
      getWishUser(that);
    })
  },
  //去开通
  toPromoCode: function (e) {
    var cardtype = e.currentTarget.dataset.cardtype;
    wx.navigateTo({
      url: '../payOrder/payOrder?cardType=' + cardtype
    })
  },
//激活码激活
  jihuo: function () {
    this.setData({
      jihuoHidden: false
    })
  },
  //判断是优惠码或激活码
  checkType: function (e) {
    var code = e.detail.value;
    var firstNum = code.substring(0, 1);
    var strExp = /^[A-Za-z]+$/;
    if (code) {
      if (strExp.test(firstNum) && first != 'C' && first != 'c' ) {
        this.setData({
          codeType: "1",
          code: code
        })
      } else {
        this.setData({
          codeType: "2",
          code: code
        })
      }
    }
  },
  //激活
  actviePay: function (e) {
    var that = this
    if (that.data.code) {
      if (that.data.codeType === "1") {
        //优惠码
        wx.request({
          url: app.globalData.path + '/user/voucherInfo',
          data: {
            voucherCode: that.data.code,
            userId: that.data.userInfo.userId
          },
          method: 'GET',
          success: function (res) {
            console.info(res)
            if (res.data.expired == false) {
              wx.navigateTo({
                url: '../payOrder/payOrder?cardType=3'
              })
            } else {
              that.setData({
                jihuoHidden: true,
                errorText: "优惠券无效",
                errorHidden: false
              })
            }
          }
        })
      } else if (that.data.codeType === "2") {
        //激活码
        console.info("激活码激活")
        wx.request({
          url: app.globalData.path + '/coffee/coffeePass/applyCard',
          data: {
            userId: that.data.userInfo.userId,
            applyWay: 1,
            activateCode: that.data.code
          },
          method: 'GET',
          success: function (res) {
            console.info(res);
            if (res.data.code == 0) {
              var msg = "恭喜您！\r\n已开通WISH VIP身份";
              wx.showToast({
                title: msg,
                icon: 'success',
                duration: 1500
              })
              wx.navigateTo({
                url: '../index/index?currentPage=' + currentPage
              })
            } else {
              that.setData({
                jihuoHidden: true,
                errorText: res.data.msg,
                errorHidden: false
              })
            }
          }

        })
      }
    }
  },
  closeErrorDialog: function () {
    this.setData({
      errorHidden: true
    })
  },
  cancel: function () {
    this.setData({
      jihuoHidden: true
    });
  }
})